<template>
  <div class="login">
      <el-card>
        <div class="l-header">FireFlx</div>
        <el-form>
          <el-form-item>
            <el-input placeholder="UserName:" 
            v-model="username"></el-input>
          </el-form-item>
          <el-form-item>
            <el-input 
            placeholder="Password:" 
            type='password' 
            v-model="pwd"></el-input>
          </el-form-item>
          <el-form-item>
            <el-button type="text" @click="regUser">点击注册</el-button>
          </el-form-item>
          <el-form-item>
            <el-button @click="loginUser">GO⬇️</el-button>
          </el-form-item>
        </el-form>
      </el-card>
  </div>
</template>

<script>
import NodeRSA from "node-rsa"
export default {
  data() {
    return {
      pwd:'',
      username:''
    }
  },
  methods: {
    regUser(){

    },
    loginUser(){
      const pwdOk = /^(?=\D+\d)(?=.*[a-z])(?=.*[A-Z])\w{6,14}$/.test(this.pwd);
      const usernameOk = /^[\u4e00-\u9fa5]{2,4}$/g.test(this.username)
      if(!usernameOk) return this.$message.error("账号格式错误!");
      // if(!pwdOk) return this.$message.error("密码格式错误!");
      const publicKey = localStorage.getItem("publicKey");
      const pwd = new NodeRSA(publicKey,'pkcs8-public').encrypt(this.pwd,'base64','utf-8')//🌟
      this.$http({
        method:'post',
        path:'/accounts/login',
        params:{
          username:this.username,
          pwd,
        }
      }).then(res=>{
        if(!res) return this.$message.error("账号格式错误!");
        const {data:{username,sex,avatar,phone,email,desc,_id},token} = res;
        this.$store.commit('setStoreUser',{//🌟🌟🌟
          _id,
          username,
          sex,
          avatar,
          phone,
          email,
          desc
        });
        const userStr = JSON.stringify({
          username,
          token,
          _id
        });
        document.cookie = "user="+userStr+';expires='+ 
        new Date(Date.now()+7*24*3600*1000) +
        ';path=/';
        location.pathname = '/admin/users/personal'
      })
    }
  },
}
</script>

<style>
  .login {
    width: 100%;
    height: 100%;
    background: url('../../assets/bj.png') no-repeat;
    background-size: 100% 100%;
    display: flex;
    justify-content: center;
    align-items: center;
  }
  .el-card {
    min-width: 300px;
    max-width: 35vw;
    /* height: ; */
  }
</style>